<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/content/variable.jsp"%>
<!DOCTYPE html>
<html lang="en" class="ie8 no-js">

<head>
    <meta charset="utf-8"/>
    <title></title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta content="width=device-width, initial-scale=1" name="viewport"/>
    <meta content="" name="description"/>
    <meta content="" name="author"/>
    <%@ include file="/content/head.jsp"%>
</head>
<body>

<!-- Main content -->
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="box box-primary">
                <div class="box-header with-border">
                    <h3 class="box-title">编辑菜单</h3>
                </div>
                <div class="box-body pad table-responsive">
                    <div id="toolbar" class="btn-group" style="padding-bottom:10px;">
                        <button id="btn_add" onclick="addMenuInfo()" type="button" class="btn btn-default btn-sm">
                            <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>更新
                        </button>
                    </div>
                    <div id="menuInfo" class="tree-demo"> </div>
                    <input type="hidden" readonly="readonly" id="roleId" name="roleId" value="${obj}">
                </div>
            </div>
        </div>
        <div id="fatherFrom" ></div>
        <!-- END SAMPLE TABLE PORTLET-->
    </div>
</section>
<%@ include file="/content/js.jsp"%>
</body>
<script>
    var r = []; // 权限树中被选中的叶子节点
    $(function () {
        init();
    });
	
    //生成树
    function init() {
        var roleId = $('#roleId').val();
        $.ajax({
            data : "roleId=" + roleId,
            type : "POST",
            url : "<%=path %>/role/findModelInfo",
            error : function(data) {
                alert("出错了！！:" + data);
            },
            success : function(data) {
                createPermitTree(JSON.stringify(data.rows));
            }
        });
    }

    function createPermitTree(datastr) {
        datastr = eval("" + datastr + "");
        $('#menuInfo').jstree({
            'plugins' : [ "wholerow", "checkbox", "types" ],
            'core' : {
                "themes" : {
                    "responsive" : false
                },
                'data' : datastr
            },
            "types" : {
                "default" : {
                    "icon" : "fa fa-folder icon-state-warning icon-lg"
                },
                "file" : {
                    "icon" : "fa fa-file icon-state-warning icon-lg"
                }
            },

        });
    }

    $('#menuInfo').on('changed.jstree', function(e, data) {
        r = [];
        var i, j;
        for (i = 0, j = data.selected.length; i < j; i++) {
            var node = data.instance.get_node(data.selected[i]);
            if (data.instance.is_leaf(node)) {
                r.push(node.id);
                var parentId = $('#menuInfo').jstree("get_parent",node.id);
                if(!isContains(r,parentId)){
                    r.push(parentId)
                }
            }
        }
    })

    function addMenuInfo(){
        var roleId = $('#roleId').val();
        var menuInfos = r.join(",");
        $.post("<%=path%>/role/updateModelInfo",
            {"menuInfos":menuInfos,"roleId":roleId}
            , function (data) {
            	if (data.missage == "success") {
                    modalMsg("新增成功", "success");
                    window.parent.location.reload();
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                }else if(data.missage == "userNameRepeat"){
                	 modalMsg("新增失败", "userNameRepeat");
                }else{
                	 modalMsg("更新失败", "error");
                }
            }
        );
    }

</script>

